package com.snake.vargoappstore;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;

import com.snake.mylibrary.base.BaseActivity;
import com.snake.vargoappstore.fragment.ClassFragment;
import com.snake.vargoappstore.fragment.HomeFragment;
import com.snake.vargoappstore.fragment.ManagerFragment;
import com.snake.vargoappstore.fragment.RankingFragment;
import com.snake.vargoappstore.fragment.SearchFragment;
import com.snake.vargoappstore.widget.CustomBar;
import com.snake.vargoappstore.widget.TabView;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {


    private String[] mTitle = {"首页", "排行", "分类", "搜索", "管理"};
    private int[] mIconSelect = {R.mipmap.underbar_home_press, R.mipmap.underbar_ranking_press, R.mipmap.underbar_class_press, R.mipmap.underbar_search_press, R.mipmap.underbar_manager_press};
    private int[] mIconNormal = {R.mipmap.underbar_home_normal, R.mipmap.underbar_ranking_normal, R.mipmap.underbar_class_normal, R.mipmap.underbar_search_normal, R.mipmap.underbar_manager_normal};

    private ViewPager mViewPager;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private TabView mTabView;
    private CustomBar mMainBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        mViewPager = (ViewPager) findViewById(R.id.vp_main);
        mTabView = (TabView) findViewById(R.id.tab_main);
        mMainBar = (CustomBar) findViewById(R.id.main_bar);
    }

    @Override
    protected void initData() {
        initFragment();
        MainPagerAdapter mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mainPagerAdapter);
        mViewPager.setOffscreenPageLimit(4);
        //1，检查客户端更新。２，被启动检查意图。３，账号操作。４，其他功能。
//        mViewPager.setPageTransformer(true,new DepthPageTransformer());
//        mViewPager.setPageTransformer(true,new ZoomOutPageTransformer());
        mTabView.setResource(mIconSelect,mIconNormal,mTitle);
        mTabView.setViewPager(mViewPager,mMainBar);
    }

    private void initFragment() {
        mFragments.add(new HomeFragment());
        mFragments.add(new ClassFragment());
        mFragments.add(new RankingFragment());
        mFragments.add(new SearchFragment());
        mFragments.add(new ManagerFragment());

        ManagerFragment managerFragment = (ManagerFragment)mFragments.get(4);
        managerFragment.setRadioGroup((RadioGroup) mMainBar.getChildAt(4));
    }

    @Override
    protected void initListener() {

    }

    class MainPagerAdapter extends FragmentPagerAdapter {


        MainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }



}
